<?php

namespace app\common\model;

use think\Cache;
use think\Model;


class Marea extends Model
{

    

    

    // 表名
    protected $name = 'marea';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [

    ];

    public static function getCacheTree()
    {
        return self::areaCacheHou()['tree'];
    }

    public static function getCacheAll()
    {
        return self::areaCacheHou()['all'];
    }
    public static function getCacheTree3()
    {
        return self::areaCache3()['tree'];
    }
    private static function areaCacheHou()
    {
        Cache::rm('area_hou');
        if (!Cache::get('area_hou')) {
            // 所有地区
            $all = $allData = self::useGlobalScope(false)->column('id, pid, name, level', 'id');
            // 格式化
            $tree = [];
            foreach ($allData as $pKey => $province) {
                if ($province['level'] === 1) {    // 省份
                    $tree[$province['id']] = $province;
                    unset($allData[$pKey]);
                    foreach ($allData as $cKey => $city) {
                        if ($city['level'] === 2 && $city['pid'] === $province['id']) {    // 城市
                            $tree[$province['id']]['city'][$city['id']] = $city;
                            unset($allData[$cKey]);
                            foreach ($allData as $rKey => $area) {
                                if ($area['level'] === 3 && $area['pid'] === $city['id']) {    // 地区
                                    $tree[$province['id']]['city'][$city['id']]['area'][$area['id']] = $area;
                                    unset($allData[$rKey]);
                                }
                            }
                        }
                    }
                }
            }
            Cache::set('area_hou', compact('all', 'tree'));
        }
        return Cache::get('area_hou');
    }

    private static function areaCache3()
    {
        if (!Cache::get('area3')) {
            // 所有地区
            $all = $allData = self::useGlobalScope(false)->column('id, pid, name, level', 'id');
            // 格式化
            $tree = [];
            foreach ($allData as $pKey => $area) {

                if ($area['level'] === 3) { // 地区
                    $tree[$area['id']] = $area;
                    unset($allData[$pKey]);
//                    foreach ($allData as $rKey => $county) {
//                        if ($county['level'] === 4 && $county['pid'] === $area['id']) { //
//                            $tree[$area['id']]['county'][$county['id']] = $county;
//                            unset($allData[$rKey]);
//                        }
//                    }



                }

            }
            Cache::set('area3', compact('all', 'tree'));
        }
        return Cache::get('area3');
    }
    protected $resultSetType='collection';
    public static function areaCache_all()
    {


        if (!Cache::get('area_data')) {
            // 所有地区
            $allData = self::cache(true)->field('id,pid,name')->with('children.children')->where(['pid'=>0])->select()->toArray();
            Cache::set('area_data', $allData);


            Cache::set('area_data', $allData);
        }
        return Cache::get('area_data');
    }
    public static function cityCache_all()
    {
        Cache::get('city_data',null);
        if (!Cache::get('city_data')) {
            // 所有地区
            $allData = self::cache(true)->with('children.children')->where(['level'=>1])->select()->toArray();
             foreach ($allData as $k=>$v){
                 foreach ($allData[$k]['children'] as $k1=>$v1){
                     $all=['id'=>'0','name'=>'全部','pid'=>0,'level'=>3];
                     $v1['children']=array_merge($all,$v1['children']);
                 }
             }

            Cache::set('city_data', $allData);
        }
        return Cache::get('city_data');
    }
    public function children()
    {
        return $this->hasMany('app\common\model\Marea','pid','id')->field('id,pid,name');
    }

    







}
